package com.llly.service.impl;


import com.llly.mapper.ProductMapper;
import com.llly.mapper.ShoppingCarMapper;
import com.llly.pojo.Easybuy_product;
import com.llly.pojo.Easybuy_user_address;
import com.llly.pojo.ShoppingCar;
import com.llly.service.ShoppingCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ShoppingCarServiceImpl implements ShoppingCarService {
    
    @Autowired
    private ShoppingCarMapper shoppingCarMapper;
    
    @Autowired
    private ProductMapper easybuyProductMapper;
    
    @Override
    public int addShoppingCar(ShoppingCar sc) {
        System.out.println("提供者获取到购物车："+sc);
        // pid  number uid ep
        //这里还有根据商品id查询 商品的单价 计算出小计
        Easybuy_product product = easybuyProductMapper.findProductByid(sc.getPid());
       
        sc.setEp(product);
    
        //根据当前获取购物车信息  商品编号   商品数量   uid
        //查询数据库 购物车，如果该商品编号有，就进行数量修改，否则就新增
        
        //select count(1) from shopping where pid=#{pid} and uid=#{uid} and isdelete=0
        System.out.println("传入提供者的购物车："+sc);
        int count= shoppingCarMapper.checkShopCar(sc.getPid(),sc.getUid());
        if(count>0){
            //调用修改方法 update shopping set number=number+n,xiaoji=xiaoji+xj
            // where and uid=#{uid} and isdelete=0 and pid=#{pid}
            //sc.setXiaoji(sc.getXiaoji()+product.getPrice()*sc.getNumber());
            return shoppingCarMapper.updateShopCarNumber(sc.getPid(),sc.getUid(),sc.getXiaoji(),sc.getNumber());
        }else{
            //新增
            
            return shoppingCarMapper.addShoppingCar(sc);
        }
        
        
       
    }
    
    @Override
    public Map<String,Object> showAllShoppingCar(Integer uid) {
        Map<String,Object> map=new HashMap<>();
        List<ShoppingCar> shoppingCars = shoppingCarMapper.showAllShoppingCar(uid);
        if (!shoppingCars.isEmpty()){
            map.put("cars",shoppingCars);
            map.put("zj",shoppingCarMapper.getTotal(uid));
        }else{
            return null;
        }
        return map;

    }

    @Override
    public List<Easybuy_user_address> getaddress(Integer uid) {
        return shoppingCarMapper.getaddress(uid);
    }

    @Override
    public int addAddress(Integer uid, String address, String remark) {
        return shoppingCarMapper.addAddress(uid, address, remark);
    }

    @Override
    public int updateCar(Integer uid) {
        return shoppingCarMapper.updateCar(uid);
    }

    @Override
    public int delCar(Integer uid, Integer pid) {
        return shoppingCarMapper.delCar(uid,pid);
    }

    @Override
    public int xiugaikucun(Integer num, Integer pid) {
        return shoppingCarMapper.xiugaikucun(num,pid);
    }

}
